package com.sohu.www;

import javax.swing.*;
import java.awt.*;
import java.time.temporal.JulianFields;

/**
 * ClassName: Calc
 * Package: com.sohu.www
 * Description:
 *
 * @Author 蒋林康
 * @Create 2023/9/20 11:05
 * @Version 1.0
 */
public class Calc extends JFrame {
    //北边的文本框
    public JPanel createPN(){
        JPanel pn=new JPanel();
        pn.setLayout(new BorderLayout(5,5));
        JTextField tfNumber=new JTextField();
        pn.add(tfNumber,BorderLayout.CENTER);
        return pn;
    }
    //西边的5个按钮
    public  JPanel createPW(){
        JPanel pw=new JPanel();
        pw.setLayout(new GridLayout(5,1,5,5));
        JButton[] jbts=new JButton[5];
        String[] labels=new String[]{"","MC","MR","MS","M+"};
        for (int i = 0; i < jbts.length; i++) {
            JButton jbt=new JButton(labels[i]);
            jbt.setForeground(Color.red);
            pw.add(jbt);
        }
        return pw;
    }
    //中间面板
    public JPanel createPC(){
        JPanel pc=new JPanel();
        pc.setLayout(new BorderLayout(5,5));
        pc.add(createPCN(),BorderLayout.NORTH);
        pc.add(createPCC(),BorderLayout.CENTER);
        return pc;
    }
    //中间面板中北边的3个按钮
    public JPanel createPCN(){
        JPanel pcn=new JPanel();
        pcn.setLayout(new GridLayout(1,3,5,5));
        JButton[] jbts=new JButton[3];
        String[] labels=new String[]{"Backspace","CE","C"};
        for (int i = 0; i < jbts.length; i++) {
            JButton jbt=new JButton(labels[i]);
            jbt.setForeground(Color.red);
            pcn.add(jbt);
        }
        return pcn;
    }
    //中间面板中的中间20个按钮
    public JPanel createPCC(){
        JPanel pcc=new JPanel();
        pcc.setLayout(new GridLayout(4,5,5,5));
        JButton[] jbts=new JButton[20];
        String[] labels=new String[]{"7","8","9","/","sqrt",
        "4","5","6","*","%",
        "1","2","3","-","1/x",
        "0","+/-",".","+","="};
        for (int i = 0; i < jbts.length; i++) {
            JButton jbt=new JButton(labels[i]);
            if (labels[i].endsWith("+")||labels[i].endsWith("-")||
            labels[i].endsWith("*")||labels[i].endsWith("/")){
                jbt.setForeground(Color.red);
            }else {
                jbt.setForeground(Color.blue);
            }
            pcc.add(jbt);
        }
        return pcc;
    }
    public Calc(){
        this.setLayout(new BorderLayout(5,5));
        this.add(createPN(),BorderLayout.NORTH);
        this.add(createPW(),BorderLayout.WEST);
        this.add(createPC(),BorderLayout.CENTER);
        this.setSize(400,250);
        this.setVisible(true);
    }

    public static void main(String[] args)throws Exception {
        //使用Windows风格
        String win="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        UIManager.setLookAndFeel(win);
        Calc calcFrm=new Calc();
    }
}
